home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / ALG2.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  5KB  |  184 lines

  1. /**************************************************************************
  2.  *
  3.  * alg2.cpp - test program for STL generic algorithm that search for
  4.  *    elements that satisfy a condition.  Section 12.3
  5.  *
  6.  * $Id: alg2.cpp,v 1.2 1995/08/29 18:42:51 oberg Exp $
  7.  *
  8.  * $$RW_INSERT_HEADER "slyrs.cpp"
  9.  *
  10.  **************************************************************************/
  11.  
  12. # include <iostream.h>
  13. # include <vector>
  14. # include <list>
  15. # include <algorithm>
  16.  
  17. # include <ctype.h>
  18. # include <string>
  19. # include <string.h>
  20.  
  21. using namespace std;
  22.  
  23. int randomInteger(int m)
  24. {    return rand() % m; }
  25.  
  26. bool isLeapYear (unsigned int year)
  27. {
  28.     if (year % 1000 == 0)
  29.         return true;
  30.     if (year % 100 == 0)
  31.         return false;
  32.     if (year % 4 == 0)
  33.         return true;
  34.     return false;
  35. }
  36.  
  37. bool operator < (const string & left, const string & right)
  38. {    return left.compare(right) < 0; }
  39.  
  40. void split
  41.     (const string & text, const string & separators, list<string> & words)
  42. {
  43.     int n = text.length();
  44.     int start, stop;
  45.     
  46.     start = text.find_first_not_of(separators);
  47.     while ((start >= 0) && (start < n)) {
  48.         stop = text.find_first_of(separators, start);
  49.         if ((stop < 0) || (stop > n)) stop = n;
  50.         words.push_back (text.substr(start, stop-start));
  51.         start = text.find_first_not_of(separators, stop+1);
  52.         }
  53.         
  54. }
  55.  
  56.  
  57. void find_test()
  58.     // illustrate use of the find function
  59. {
  60.     cout << "Test of algorithm find" << endl;
  61.     int vintageYears[] = {1967, 1972, 1974, 1980, 1995};
  62.  
  63.     vector<int>::iterator start = vintageYears;
  64.     vector<int>::iterator stop = vintageYears + 5;
  65.     
  66.     vector<int>::iterator where = find_if(start, stop, isLeapYear);
  67.     
  68.     if (where != stop)
  69.         cout << "first vintage leap year is " << *where << endl;
  70.     else
  71.         cout << "no vintage leap years" << endl;
  72.         
  73.     where = find(start, stop, 1995);
  74.     
  75.     if (where != stop)
  76.         cout << "1995 is position " << where - start << " in sequence" << endl;
  77.     else
  78.         cout << "1995 does not occur in sequence" << endl;
  79.     
  80. }
  81.  
  82. void find_adjacent_test()
  83. {
  84.     cout << "Test of algorithm find adjacent" << endl;
  85.     char * text = "The bookkeeper carefully opened the door";
  86.     
  87.     vector<char>::iterator start = text;
  88.     vector<char>::iterator stop = text + strlen(text);
  89.     
  90.     vector<char>::iterator where = start;
  91.     
  92.     cout << "In the text " << text << endl;
  93.     while ((where = adjacent_find(where, stop)) != stop) {
  94.         cout << "double " << *where << " in position " << where - start << endl;
  95.         ++where;
  96.         }
  97. }
  98.  
  99. void search_test()
  100.     // illustrate the use of the search function
  101. {
  102.     cout << "Test of algorithm search" << endl;
  103.     char * base = "aspirations";
  104.     char * text = "ration";
  105.     
  106.     char * where = search(base, base + strlen(base), text, text + strlen(text));
  107.     
  108.     if (*where != '\0')
  109.         cout << "substring begins in position " << where - base << endl;
  110.     else
  111.         cout << "substring does not occur in text" << endl;
  112. }
  113.  
  114. void max_min_example ()
  115.     // illustrate use of max_element and min_element algorithms
  116. {
  117.     cout << "Test of max and min algorithms " << endl;
  118.     // make a vector of random numbers between 0 and 99
  119.     vector<int> numbers(25);
  120.     for (int i = 0; i < 25; i++)
  121.         numbers[i] = randomInteger(100);
  122.         
  123.     // print the maximum
  124.     vector<int>::iterator max =
  125.         max_element(numbers.begin(), numbers.end());
  126.     cout << "largest value was " << *max << endl;
  127.     
  128.         // example using strings
  129.     string text =
  130.         "it was the best of times, it was the worst of times.";
  131.     list<string>words;
  132.     split(text, " .,!:;", words);
  133.     cout << "The smallest word is "
  134.         << * min_element(words.begin(), words.end())
  135.         << " and the largest word is "
  136.         << * max_element(words.begin(), words.end())
  137.         << endl;
  138. }
  139.  
  140. void mismatch_test(char * a, char * b)
  141.     // illustrate the use of the mismatch function
  142. {
  143.     pair<char *, char *> differPositions(0, 0);
  144.     char * aDiffPos;
  145.     char * bDiffPos;
  146.  
  147.     if (strlen(a) < strlen(b)) {
  148.         differPositions = mismatch(a, a + strlen(a), b);
  149.         aDiffPos = differPositions.first;
  150.         bDiffPos = differPositions.second;
  151.         }
  152.     else {
  153.         differPositions = mismatch(b, b + strlen(b), a);
  154.         aDiffPos = differPositions.second;
  155.         bDiffPos = differPositions.first;
  156.         }
  157.         
  158.     cout << "string " << a;
  159.     if (*aDiffPos == *bDiffPos)
  160.         cout << " is equal to ";
  161.     else if (*aDiffPos < *bDiffPos)
  162.         cout << " is less than ";
  163.     else
  164.         cout << " is greater than ";
  165.     cout << b << endl;
  166. }
  167.  
  168. int main()
  169. {
  170.     cout << "STL generic algorithms -- Searching Algorithms" << endl;
  171.     find_test();
  172.     find_adjacent_test();
  173.     search_test;
  174.     max_min_example();
  175.     mismatch_test("goody", "goody");
  176.     mismatch_test("good", "goody");
  177.     mismatch_test("goody", "good");
  178.     mismatch_test("good", "fred");
  179.     mismatch_test("fred", "good");
  180.     
  181.     cout << "End of search algorithms test program" << endl;
  182.     return 0;
  183. }
  184.